package com.dc.commons.shiro.listener;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationListener;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListenerAdapter;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dc.commons.helper.DateHelper;
import com.dc.commons.shiro.session.DcSession;
/**
 * *****************************************************************************
 * 文件名: DcUserListener.java <br>
 * 版本: 1.0<br>
 * 描述: 用户监听类<br>
 * 		当用户 登录成功，退出，超时退出的时候触发此类
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2015年12月14日 下午11:44:03<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 *****************************************************************************
 */
import com.dc.commons.sys.persistence.User;
public class DcUserListener extends SessionListenerAdapter implements AuthenticationListener,DcSessionListener{

	private static final Logger logger=LoggerFactory.getLogger(DcUserListener.class);
	
	
	private SessionDAO sessionDao;
	
	public DcUserListener(SessionDAO sessionDao){
		this.sessionDao=sessionDao;
	}
	
	@Override
	public void onSuccess(AuthenticationToken token, AuthenticationInfo info) {
		logger.debug("{} is login success",token.getPrincipal());
		
	}

	@Override
	public void onFailure(AuthenticationToken token, AuthenticationException ae) {
		logger.debug(" is login failure cause {}",token.getPrincipal(),ae.getMessage());
		
	}

	@Override
	public void onLogout(PrincipalCollection principals) {
		Session shiroSession=SecurityUtils.getSubject().getSession(false);
		if(principals!=null){
			User user=(User)principals.getPrimaryPrincipal();
			DcSession dcSession=(DcSession)sessionDao.readSession(shiroSession.getId());
    		dcSession.validate();
    		if(!dcSession.isDcValid()){
    			logger.info("{} is logout but  is {} ",user.getUserCode(),dcSession.getMsg());
    		}else{
    			logger.info("{} is logout ",user.getUserCode());
    		}
		}
	}

	/**
	 * 超时Session 事件
	 */
	@Override
	public void onExpiration(Session session) {
		PrincipalCollection existingPrincipals =  (PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
		if(existingPrincipals!=null){
			User user=(User)existingPrincipals.getPrimaryPrincipal();
			DcSession dcSession=(DcSession)sessionDao.readSession(session.getId());
    		dcSession.validate();
    		if(dcSession.isDcValid()){
    			logger.info("{} is expiration ",user.getUserCode());
    		}else{
    			
    		}
		}
	}

	/**
	 * 强退事件
	 * @param source 强退着
	 * @param target 被强退着
	 * @param dcSession session
	 * @param host 强退 IP
	 */
	public void onForced(User source,User target,DcSession dcSession,String host){
		logger.info("{} 于  {}  在 {} 将 {}  踢下。",source.getUserCode(),DateHelper.formatDateTime(),host,target.getUserCode());
	}

}
